package es.xpt.ysf.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import es.xpt.ysf.model.Item;

public class TimeZones {

	public static List<Item> getTimeZone(String offset, Locale locale) {
		List<Item> timeZones = new ArrayList<Item>();
		for (String currentTimeZone : TimeZone.getAvailableIDs(Integer.valueOf(offset))) {
			TimeZone tz = TimeZone.getTimeZone(currentTimeZone);
			Item i = new Item(currentTimeZone, null);
			i.setShortName(currentTimeZone  +" - "+ tz.getDisplayName(locale));
			timeZones.add(i);
			
		}
		return timeZones;
	}
	
	public static List<Item> getTimeOffsets() {
		
		List<Item> rawoffsets = new ArrayList<Item>();
		for (String currentTimeZone : TimeZone.getAvailableIDs()) {
			TimeZone tz = TimeZone.getTimeZone(currentTimeZone);
			Item i = new Item(String.valueOf(tz.getRawOffset()), null);
			if ( !rawoffsets.contains(i)) {
				int hour = tz.getRawOffset() / 3600000;
				int minutes = (Math.abs(tz.getRawOffset()) % 3600000) / 60000;
				i.setShortName("GMT" + ( hour<0 ? "-" : hour==0 ? " " : "+" ) + (Math.abs(hour)<10 ? "0" : "") + Math.abs(hour) 
						+ ":" + (minutes == 0 ? "00" : (minutes < 10 ? "0" :"") + minutes));
				rawoffsets.add(i);
			}
		}
		return rawoffsets;
	}
	
}
